/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.xd.manager.impl;

import com.xy.biz.fd.client.DocumentClient;
import com.xy.biz.fd.domain.req.document.GetDocumentRequest;
import com.xy.biz.fd.domain.resp.GetDocumentResponse;
import com.xy.biz.xd.dao.DocumentFlowDao;
import com.xy.biz.xd.dao.DocumentFlowHandleDao;
import com.xy.biz.xd.dao.DocumentFlowHandleUserDao;
import com.xy.biz.xd.domain.bo.DocumentFlowBO;
import com.xy.biz.xd.domain.bo.DocumentFlowHandleBO;
import com.xy.biz.xd.domain.bo.DocumentFlowHandleUserBO;
import com.xy.biz.xd.domain.converter.DocumentFlowConverter;
import com.xy.biz.xd.domain.converter.DocumentFlowHandleConverter;
import com.xy.biz.xd.domain.converter.DocumentFlowHandleUserConverter;
import com.xy.biz.xd.domain.po.DocumentFlowPO;
import com.xy.biz.xd.domain.query.DocumentFlowQuery;
import com.xy.biz.xd.domain.req.SaveDocumentFlowHandleUserRequest;
import com.xy.biz.xd.manager.DocumentFlowManager;
import com.xy.lang.util.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 *
 *
 * @author zsp
 * @date 2021-1-22
 */
@Service
public class DocumentFlowManagerImpl implements DocumentFlowManager {

    @Autowired
    private DocumentClient documentClient;
    @Autowired
    private DocumentFlowDao documentFlowDao;
    @Autowired
    private DocumentFlowHandleDao documentFlowHandleDao;

    @Transactional
    @Override
    public int saveDocumentFlow(DocumentFlowBO documentFlowBO) {
        DocumentFlowPO documentFlowPO = DocumentFlowConverter.toPO(documentFlowBO);
        int affectedRows = documentFlowDao.saveDocumentFlow(documentFlowPO);
        if (affectedRows > 0) {
            List<DocumentFlowHandleBO> documentFlowHandleBOList = documentFlowBO.getDocumentFlowHandleBOList();
            if (!CollectionUtils.isEmpty(documentFlowHandleBOList)) {
                documentFlowHandleDao.saveAllDocumentFlowHandle(DocumentFlowHandleConverter.toPO(documentFlowHandleBOList));
            }
        }
        return affectedRows;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveAllDocumentFlow(List<DocumentFlowBO> collection) {
        List<DocumentFlowPO> documentFlowPOList = DocumentFlowConverter.toPO(collection);
        int affectedRows = documentFlowDao.saveAllDocumentFlow(documentFlowPOList);
        if (affectedRows > 0) {
            List<DocumentFlowHandleBO> documentFlowHandleBOList = collection
                    .stream()
                    .filter(e -> !CollectionUtils.isEmpty(e.getDocumentFlowHandleBOList()))
                    .flatMap(e -> e.getDocumentFlowHandleBOList()
                            .stream())
                    .collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(documentFlowHandleBOList)) {
                documentFlowHandleDao.saveAllDocumentFlowHandle(DocumentFlowHandleConverter.toPO(documentFlowHandleBOList));
            }
        }
        return affectedRows;
    }


    @Override
    public int updateDocumentFlow(DocumentFlowBO documentFlowBO) {
        return documentFlowDao.updateDocumentFlow(DocumentFlowConverter.toPO(documentFlowBO));
    }

    @Override
    public int updateAllDocumentFlow(List<DocumentFlowBO> collection) {
        return documentFlowDao.updateAllDocumentFlow(DocumentFlowConverter.toPO(collection));
    }

    @Override
    public int deleteDocumentFlow(Long documentFlowId) {
        return documentFlowDao.deleteDocumentFlow(documentFlowId);
    }

    @Override
    public DocumentFlowBO getDocumentFlow(Long documentFlowId) {
        DocumentFlowBO documentFlowBO = DocumentFlowConverter.fromPO(documentFlowDao.getDocumentFlow(documentFlowId));
        if (Objects.nonNull(documentFlowBO) && Objects.isNull(documentFlowBO.getFileExtension())) {
            GetDocumentResponse document = ResultUtils.getOrException(documentClient.getDocument(GetDocumentRequest.builder()
                    .documentId(documentFlowBO.getDocumentId())
                    .build()));
            if (Objects.nonNull(document)) {
                documentFlowBO.setFileExtension(document.getFileExtension());
            }
        }
        return documentFlowBO;
    }

    /**
     * 查找一个
     *
     * @param documentFlowQuery
     * @return
     */
    @Override
    public DocumentFlowBO getDocumentFlow(DocumentFlowQuery documentFlowQuery) {
        return DocumentFlowConverter.fromPO(documentFlowDao.getDocumentFlowByBO(documentFlowQuery));
    }

    @Override
    public List<DocumentFlowBO> listDocumentFlowById(Collection<Long> collection) {
        return DocumentFlowConverter.fromPO(documentFlowDao.listDocumentFlowById(collection));
    }

    @Override
    public List<DocumentFlowBO> listDocumentFlow(DocumentFlowQuery query) {
        return DocumentFlowConverter.fromPO(documentFlowDao.listDocumentFlow(query));
    }

    @Override
    public int countDocumentFlow(DocumentFlowQuery query) {
        return documentFlowDao.countDocumentFlow(query);
    }

}
